<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Admin\Api\NewmemberApi;
use Admin\Api\RoleApi;
use Think\Controller;
use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Weixin\Api\WxuserApi;
use Admin\Api\UserPictureApi;
use Shop\Api\AddressApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrdersContactInfoApi;
class TestPayController extends HomeController {
        /*
         * 启用HTML5进行支付
         * */
    /*微信支付HTML5的实现*/
    public function jsapi(){
        $map['order_code'] = I('order_code');
        $this->assign('order_code',$map['order_code']);

        //查找订单详情
        $orderres = apiCall(OrdersApi::GET_INFO,array($map));

        if($orderres['status']){
            $xctime = time()-$orderres['info']['createtime'];
            if($xctime < 7200){
                $body = '藏味优品-购买了商品';
                //$fee = bcadd($orderres['info']['price'],$orderres['info']['post_price'],4)*100;
                $fee = bcadd($orderres['info']['price'],$orderres['info']['post_price'],2);
                $fee = bcsub($fee,$orderres['info']['pay_balance'],2);
                $money =  bcsub($fee,$orderres['info']['pay_score']/100,2);

                $this->assign('money',$money);
                $fee = $money*100;
                $config = C("WXPAY_PAY_CONFIG");
                //①、获取用户openid
                $tools = new \Common\Api\Wxpay\JsApi($config);
                $openId =$this->userinfo['openid'];//获得用户的OPenid
                //②、统一下单
                $input = new \Common\Api\Wxpay\WxPayUnifiedOrder();
                $input->setConfig($config);
                $input->SetBody($body);
                $input->SetAttach("test2");
                $input->SetOut_trade_no($map['order_code']);//随机生成订单号
                $input->SetTotal_fee($fee);
                $input->SetTime_start(date("YmdHis"));
                $input->SetTime_expire(date("YmdHis", time() + 36000));

                $input->SetNotify_url("http://91cangwei.com/index.php/Home/Ajaxinform/ajaxinform");
                $input->SetTrade_type("JSAPI");
                $input->SetOpenid($openId);

                \Common\Api\Wxpay\WxPayApi::setConfig($config);

                $order = \Common\Api\Wxpay\WxPayApi::unifiedOrder($input);

                $jsApiParameters = $tools->GetJsApiParameters($order);

                $this->assign("jsApiParameters",$jsApiParameters);
                $this->display();
            }else{
                $this->error('对不起订单已经失效了');
            }

        }else{
            $this->error('不好意思 支付页跳转失败',U('Index/index'));
        }

    }


    /*
     * 确认纯储值支付
     *
     * */
    public function store_pay(){

    }





}
